package com.krain.ddbb.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.krain.corelibrary.util.SpUtil;
import com.krain.corelibrary.widget.recyleview.RefreshRecyclerView;
import com.krain.ddbb.R;
import com.krain.ddbb.adapter.RobOrderListAdapter;
import com.krain.ddbb.base.BaseActivity;
import com.krain.ddbb.constant.SpKeys;
import com.krain.ddbb.entity.AppApi;
import com.krain.ddbb.entity.RobOrderBean;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.List;

/**
 * Created by Nick.Hu on 2016/10/17.
 * Mail:huyamin@yonghui.cn | huyamin@hotmail.com
 * Tel: +8618521550285
 */
@EActivity(R.layout.activity_rob_orderlist)
public class RobOrderListActivity extends BaseActivity implements RefreshRecyclerView.SwipeRefreshListener{
    @ViewById(R.id.order_list)
    RefreshRecyclerView mRecyclerView;
    int pageIndex;
    List<RobOrderBean> mList;
    RobOrderListAdapter adapter;

    @Override
    protected String getToolbarTitle() {
        return getString(R.string.rob_order_list);
    }


    @AfterViews
    void afterView(){
        mRecyclerView.showRefreshing(true);
        mRecyclerView.setSwipeRefreshListener(this);
        request();
    }

    public static void jumpTothisActivity(Context context){
        context.startActivity(new Intent(context,RobOrderListActivity_.class));
    }

    @Background
    void request(){
        if (TextUtils.isEmpty(SpUtil.getString(SpKeys.LNG,""))|| TextUtils.isEmpty(SpUtil.getString(SpKeys.LAT,""))){
            baseUtil.showSnackBar(this,"无法获取定位数据，请检查您的定位权限是否打开");
            return;
        }
        mList =  AppApi.getsInstance(getApplicationContext()).getUnRobOrderList(app.getmUserinfo().getAccess_token(),SpUtil.getString(SpKeys.LNG,""),SpUtil.getString(SpKeys.LAT,""),pageIndex).getList();
       setAdapter();
    }

    @UiThread
    void setAdapter(){
        if(pageIndex == 0){
            adapter = new RobOrderListAdapter(mList,this,app,baseUtil);
            mRecyclerView.setAdapter(adapter);
            if (adapter.getItemCount()>3)  mRecyclerView.setIsLoadMore(true);
            else mRecyclerView.setIsLoadMore(false);
        }else {
            adapter.notifyDataSetChanged();
        }
        mRecyclerView.showRefreshing(false);
    }

    @Override
    public void onLoadMore() {
        pageIndex++;
        request();
    }

    @Override
    public void onRefresh() {
        pageIndex = 0;
        request();
    }
}
